<script setup lang="ts">
import user from '@/stores/user'
import { ref, reactive } from 'vue'
import type { Rule } from 'ant-design-vue/es/form'
import Upload from '@/components/upload/index.vue'
import { updataUser } from '@/api/user/index'
import { message } from 'ant-design-vue'

const userStores = user()

//表单内容
const formState = reactive({
  username: '',
  password: '',
  name: '',
  isAdministrator: 0,
  sex: '男',
  avatar: ''
})
//表单元素
const FormEl = ref()
//控制对话框
const open = ref(false)
const id = Number(localStorage.getItem('book-user-id'))
//确认按钮回调
const handleOk = () => {
  //表单验证
  FormEl.value
    .validateFields()
    .then(async () => {
      //验证通过
      open.value = false
      //发送请求
      let res = await updataUser(id, formState)
      if (res.code === 200) {
        message.success('修改成功', 2)
        //重新获取信息
        userStores.getUser(id)
      }
    })
    .catch((err) => {
      //验证失败
      console.log(err)
    })
}
//头像地址
let picture = ref('')
//上传头像回调
const uploadSuccess = (url: string) => {
  if (url) {
    formState.avatar = url
  }
}
//表单验证规则
const publicRules = { required: true, message: '不能为空' }
const rules: Record<string, Rule[]> = {
  username: [publicRules, { min: 2, max: 20, message: '长度为2-20个字符' }],
  password: [publicRules, { min: 6, max: 20, message: '长度为6-20个字符' }],
  name: [publicRules, { min: 2, max: 20, message: '长度为2-20个字符' }],
  avatar: [publicRules]
}
//修改按钮回调
const uploadBtn = () => {
  open.value = true
  //移除表单校验结果
  FormEl.value?.clearValidate()
  const { name, username, password, sex, avatar, isAdministrator } = userStores
  formState.name = name
  formState.username = username
  formState.password = password
  formState.sex = sex
  formState.avatar = avatar
  formState.isAdministrator = isAdministrator
  picture.value = avatar
}
</script>

<script lang="ts">
export default {
  name: 'User'
}
</script>

<template>
  <div class="user-profile">
    <div class="card-container">
      <!-- 头像区域 -->
      <div class="avatar-section">
        <div class="avatar-wrapper">
          <img 
            :src="userStores.avatar || '/default-avatar.png'" 
            alt="用户头像" 
            class="user-avatar"
          />
        </div>
      </div>
      
      <!-- 信息展示区域 -->
      <div class="info-section">
        <div class="user-info">
          <div class="info-row">
            <span class="label">姓名:</span>
            <span class="value">{{ userStores.name }}</span>
          </div>
          <div class="info-row">
            <span class="label">账号:</span>
            <span class="value">{{ userStores.username }}</span>
          </div>
          <div class="info-row">
            <span class="label">密码:</span>
            <span class="value">{{ userStores.password }}</span>
          </div>
          <div class="info-row">
            <span class="label">身份:</span>
            <span class="value">{{ userStores.isAdministrator ? '管理员' : '普通用户' }}</span>
          </div>
          <div class="info-row">
            <span class="label">性别:</span>
            <span class="value">{{ userStores.sex }}</span>
          </div>
        </div>
        
        <div class="action-container">
          <a-button 
            type="primary" 
            size="large" 
            @click="uploadBtn"
            class="edit-button"
          >
            修改
          </a-button>
        </div>
      </div>
    </div>
    
    <!-- 对话框 -->
    <a-modal v-model:open="open" title="修改" @ok="handleOk" class="custom-modal">
      <a-form
        :model="formState"
        name="basic"
        :label-col="{ span: 8 }"
        :wrapper-col="{ span: 16 }"
        autocomplete="off"
        :rules="rules"
        ref="FormEl"
        class="custom-form"
      >
        <a-form-item label="账号" name="username">
          <a-input v-model:value="formState.username" disabled class="disabled-input" />
        </a-form-item>

        <a-form-item label="密码" name="password">
          <a-input-password v-model:value="formState.password" />
        </a-form-item>
        
        <a-form-item label="姓名" name="name">
          <a-input v-model:value="formState.name" />
        </a-form-item>
        
        <a-form-item label="性别">
          <a-radio-group name="radioGroup" v-model:value="formState.sex" class="radio-group">
            <a-radio value="男">男</a-radio>
            <a-radio value="女">女</a-radio>
          </a-radio-group>
        </a-form-item>
        
        <a-form-item label="头像" name="avatar">
          <Upload
            @uploadSuccess="uploadSuccess"
            :imgSrc="picture"
            :open="{ open }"
            class="custom-upload"
          />
        </a-form-item>
      </a-form>
    </a-modal>
  </div>
</template>

<style scoped lang="less">
/* 定义颜色变量 */
@primary-color: #1677ff;
@text-color: #333;
@text-secondary-color: #666;
@border-color: #e5e7eb;
@bg-color: #f8fafc;

.user-profile {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  padding: 2rem;
  max-width: 1200px;
  margin: 0 auto;
  
  /* 卡片容器 */
  .card-container {
    background-color: white;
    border-radius: 1rem;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
    padding: 2rem;
    display: flex;
    flex-direction: column;
    gap: 2rem;
    
    @media (min-width: 768px) {
      flex-direction: row;
    }
  }
  
  /* 头像区域 */
  .avatar-section {
    display: flex;
    justify-content: center;
    
    .avatar-wrapper {
      position: relative;
      
      .user-avatar {
        width: 120px;
        height: 120px;
        border-radius: 50%;
        object-fit: cover;
        border: 4px solid @primary-color;
        box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
        transition: transform 0.3s ease;
        
        &:hover {
          transform: scale(1.05);
        }
      }
    }
  }
  
  /* 信息展示区域 */
  .info-section {
    flex: 1;
    
    .user-info {
      display: grid;
      grid-template-columns: 1fr;
      gap: 1rem;
      
      @media (min-width: 576px) {
        grid-template-columns: 1fr 1fr;
      }
      
      .info-row {
        display: flex;
        align-items: center;
        gap: 1rem;
        
        .label {
          font-weight: 500;
          color: @text-secondary-color;
          min-width: 80px;
        }
        
        .value {
          color: @text-color;
          font-size: 1rem;
        }
      }
    }
    
    .action-container {
      margin-top: 1.5rem;
      text-align: center;
      
      @media (min-width: 768px) {
        text-align: left;
      }
      
      .edit-button {
        padding: 0.5rem 1.5rem;
        border-radius: 0.5rem;
        background-color: @primary-color;
        color: white;
        font-weight: 500;
        transition: all 0.2s ease;
        
        &:hover {
          background-color: darken(@primary-color, 10%);
          transform: translateY(-2px);
          box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
        }
      }
    }
  }
  
  /* 自定义模态框 */
  .custom-modal {
    .ant-modal-content {
      border-radius: 1rem;
      box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.1);
    }
    
    .ant-modal-header {
      border-radius: 1rem 1rem 0 0;
      background-color: @bg-color;
    }
    
    .ant-modal-title {
      font-weight: 600;
      color: @text-color;
      font-size: 1.25rem;
    }
  }
  
  /* 自定义表单 */
  .custom-form {
    .ant-form-item {
      margin-bottom: 1.25rem;
    }
    
    .ant-form-item-label {
      label {
        font-weight: 500;
        color: @text-secondary-color;
      }
    }
    
    .ant-input, .ant-input-password {
      border-radius: 0.5rem;
      height: 2.5rem;
      border-color: @border-color;
      
      &:focus, &:hover {
        border-color: @primary-color;
        box-shadow: 0 0 0 2px rgba(22, 119, 255, 0.2);
      }
    }
    
    .disabled-input {
      background-color: @bg-color;
      color: #9ca3af;
    }
    
    .radio-group {
      display: flex;
      gap: 1rem;
    }
    
    .custom-upload {
      .ant-upload-select {
        width: 100%;
      }
    }
  }
}
</style>